#include <iostream>
#include <strstream>
#include <string>
#include "Cbook.h"
#include "manager.h"

void Manager::run()
{
	std::cout << "LibraryManager 0.1 on win64" << std::endl;
	std::cout << "Input \'help \' or command list" << std::endl;
	while(true)
	{
		std::string line;
		std::cout << ">>";
		std::getline(std::cin, line);

		if(line == "exit")
			break;

		if(line == "help")
		{
			std::cout << "Command List: \n\t1. add book\n\t\t >> add (type) (name)\n\t2. show table\n\t\t >> show (type)\
				\n\t3. remove book \n\t\t >> remove (type) (name)\n\t4. lend book\
				\n\t\t >> lend (type) (name) \n\t5. return book \n\t\t >> return (type) (name)" << std::endl;
			continue;
		}

		std::istrstream in(line.c_str());
		int cnt=0;
		while(in >> cmd[cnt])cnt++;

		if(!check(cnt, cmd[0]))
		{
			std::cout << "Error, please check your command!" << std::endl;
			continue;
		}

		if(cmd[0] == "add")
			add();
		else if(cmd[0] == "show")
			show();
		else if(cmd[0] == "remove")
			remove();
		else if(cmd[0] == "lend")
			lend();
		else if(cmd[0] == "return")
			turnback();

	}

	return ;
}

bool Manager::check(int cnt, std::string command)
{
	if(command == "add")
	{
		if(cnt == 3)return true;
	}
	else if(command == "show")
	{
		if(cnt == 2)return true;
	}
	else if(command == "remove")
	{
		if(cnt == 3)return true;
	}
	else if(command == "lend")
	{
		if(cnt == 3)return true;
	}
	else if(command == "return")
	{
		if(cnt == 3)return true;
	}

	return false;
}

void Manager::add()
{
	if(cmd[1] == "Normalbook")
	{
		Normalbook temp(cmd[2]);
		Normallist.push_back(temp);
	}
	else if(cmd[1] == "Magazine")
	{
		Magazine temp(cmd[2]);
		Magazinelist.push_back(temp);
	}
	else if(cmd[1] == "Spbook")
	{
		Spbook temp(cmd[2]);
		Spbooklist.push_back(temp);
	}

	show();
	return ;
}

void Manager::show()
{
	if(cmd[1] == "Normalbook")
	{
		std::cout << "Normal Book list:" << std::endl;
		for(int i = 0; i < Normallist.size(); i++)
		{
			std::cout << "#" << i << " Name:" << Normallist[i].get_name() << " State: " << (Normallist[i].get_state() ? "Not lend" : "Lend") << std::endl;
		}
	}
	else if(cmd[1] == "Magazine")
	{
		std::cout << "Magazine list:" << std::endl;
		for (int i =  0; i < Magazinelist.size(); i++) 
		{
			std::cout << "#" << i << " Name:" << Magazinelist[i].get_name() << " State: " << (Magazinelist[i].get_state() ? "Not lend" : "Lend") << std::endl;
		}
	}
	else if(cmd[1] == "Spbook")
	{
		std::cout << "Spbook list: " << std::endl;
		for (int i =  0; i < Spbooklist.size(); i++) 
		{
			std::cout << "#" << i << " Name:" << Spbooklist[i].get_name() << " State: " << (Spbooklist[i].get_state() ? "Not lend" : "Lend") << std::endl;
		}
	}

	return ;
}

void Manager::remove()
{
	if(cmd[1] == "Normalbook")
	{
		for(int i = 0; i < Normallist.size(); i++)
		{
			if(Normallist[i].get_name() == cmd[2])
			{
				Normallist.remove(i);
				std::cout << "remove done" << std::endl;
				return ;
			}
		}
	}
	else if(cmd[1] == "Magazine")
	{
		for (int i =  0; i < Magazinelist.size(); i++) 
		{
			if(Magazinelist[i].get_name() == cmd[2])
			{
				Magazinelist.remove(i);
				std::cout << "remove done" << std::endl;
				return ;
			}
		}
	}
	else if(cmd[1] == "Spbook")
	{
		for (int i =  0; i < Spbooklist.size(); i++) 
		{
			if(Spbooklist[i].get_name() == cmd[2])
			{
				Spbooklist.remove(i);
				std::cout << "remove done" << std::endl;
				return ;
			}
		}
	}

	std::cout << "Error! Can\'t find your book";
	return ;
}

void Manager::lend()
{
	if(cmd[1] == "Normalbook")
	{
		for(int i = 0; i < Normallist.size(); i++)
		{
			if(Normallist[i].get_name() == cmd[2])
			{
				Normallist[i].change();
				return ;
			}
		}
	}
	else if(cmd[1] == "Magazine")
	{
		for (int i =  0; i < Magazinelist.size(); i++) 
		{
			if(Magazinelist[i].get_name() == cmd[2])
			{
				Magazinelist[i].change();
				return ;
			}
		}
	}
	else if(cmd[1] == "Spbook")
	{
		for (int i =  0; i < Spbooklist.size(); i++) 
		{
			if(Spbooklist[i].get_name() == cmd[2])
			{
				Spbooklist[i].change();
				return ;
			}
		}
	}

	std::cout << "Error! Can\'t find your book";
	return ;
}

void Manager::turnback()
{
	if(cmd[1] == "Normalbook")
	{
		for(int i = 0; i < Normallist.size(); i++)
		{
			if(Normallist[i].get_name() == cmd[2])
			{
				Normallist[i].change();
				return ;
			}
		}
	}
	else if(cmd[1] == "Magazine")
	{
		for (int i =  0; i < Magazinelist.size(); i++) 
		{
			if(Magazinelist[i].get_name() == cmd[2])
			{
				Magazinelist[i].change();
				return ;
			}
		}
	}
	else if(cmd[1] == "Spbook")
	{
		for (int i =  0; i < Spbooklist.size(); i++) 
		{
			if(Spbooklist[i].get_name() == cmd[2])
			{
				Spbooklist[i].change();
				return ;
			}
		}
	}

	std::cout << "Error! Can\'t find your book";
	return ;
}
